package nio.bytebuffer;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class Equals_CompareTo {

    public static void main(String[] args) {
        compareToDemo2();
    }

    private static void equalsDemo1() {
        byte[] byteArrayIn1 = { 1, 2, 3, 4, 5 };
        ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
        System.out.println(" A=" + bytebuffer1.equals(bytebuffer1)); // true
    }

    private static void equalsDemo2() {
        byte[] byteArrayIn1 = { 1, 2, 3, 4, 5 };
        int[] intArrayIn2 = { 1, 2, 3, 4, 5 };
        ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
        IntBuffer intbuffer2 = IntBuffer.wrap(intArrayIn2);
        System.out.println(" A=" + bytebuffer1.equals(intbuffer2)); // false
    }

    private static void equalsDemo3() {
        byte[] byteArrayIn1 = { 3, 4, 5 };
        byte[] byteArrayIn2 = { 1, 2, 3, 4, 5, 6, 7, 8 };
        ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
        ByteBuffer bytebuffer2 = ByteBuffer.wrap(byteArrayIn2);
        bytebuffer1.position(0);
        bytebuffer2.position(3);
        System.out.println(" A=" + bytebuffer1.equals(bytebuffer2)); // false
        System.out.println(" bytebuffer1.remaining()=" + bytebuffer1.remaining()); // bytebuffer1.remaining()=3
        System.out.println(" bytebuffer2.remaining()=" + bytebuffer2.remaining()); // bytebuffer2.remaining()=5
    }

    private static void equalsDemo4() {
        byte[] byteArrayIn1 = { 3, 4, 5 };
        byte[] byteArrayIn2 = { 1, 2, 3, 4, 5, 6, 7, 8 };
        ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
        ByteBuffer bytebuffer2 = ByteBuffer.wrap(byteArrayIn2);

        bytebuffer1.position(0);
        bytebuffer1.limit(3);

        bytebuffer2.position( 2);
        bytebuffer2.limit( 5);

        System.out.println(" A=" + bytebuffer1.equals( bytebuffer2));  // true
        System.out.println(" AA1 bytebuffer1. remaining()=" + bytebuffer1. remaining()); // 3
        System.out.println(" AA2 bytebuffer2. remaining()=" + bytebuffer2. remaining()); // 3
        bytebuffer2.put( 3, (byte)44);
        System.out.println(" B=" + bytebuffer1. equals(bytebuffer2)); // false
        System.out.println(" BB1 bytebuffer1. remaining()=" + bytebuffer1.remaining()); // 3
        System.out.println(" BB2 bytebuffer2. remaining()=" + bytebuffer2.remaining()); // 3

    }

    private static void compareToDemo() {
        byte[] byteArrayIn1 = { 3, 4, 5 };
        byte[] byteArrayIn2 = { 1, 2, 3, 104, 5, 6, 7, 8, 9, 10 };
        ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
        ByteBuffer bytebuffer2 = ByteBuffer.wrap(byteArrayIn2);
        bytebuffer1.position(0);
        bytebuffer2.position(2);
        System.out.println(" A=" + bytebuffer1.compareTo(bytebuffer2)); // -100
    }

    private static void compareToDemo2() {
        byte[] byteArrayIn1 = { 3, 4, 5 };
        byte[] byteArrayIn2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
        ByteBuffer bytebuffer2 = ByteBuffer.wrap(byteArrayIn2);
        bytebuffer1.position(0);
        bytebuffer2.position(2);
        System.out.println(" A=" + bytebuffer1.compareTo(bytebuffer2)); // -5
    }

}
